home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / osi / isode / vmsisode / vmsisode80_tar.Z / vmsisode80_tar / sockit / source / hpwd.mar < prev    next >
Encoding:
Text File  |  1988-10-24  |  8.2 KB  |  336 lines

  1. .TITLE    HPWD
  2.  
  3. ;++
  4. ; Hash Password
  5. ;
  6. ; Written by someone at DEC.  Copied by Peter Kay for DIT CSIRO
  7. ;
  8. ;--
  9.  
  10. .SBTTL    DECLARATIONS
  11.  
  12. ;
  13. ; Macros:
  14. ;
  15.  
  16. .macro    pushq    Src
  17.     movq    Src,-(sp)
  18. .endm
  19.  
  20. .macro    popq    Dst
  21.     movq    (sp)+,Dst
  22. .endm
  23.  
  24. ;
  25. ; Equated symbols
  26. ;
  27.  
  28.     OUTDSC    = 4            ; addr of encrypted output descriptor
  29.     PWDDSC    = OUTDSC + 4        ; addr of password descriptor
  30.     ENCRYPT    = PWDDSC + 4        ; Encryption algorithm index (byte)
  31.     SALT    = ENCRYPT + 4        ; random number (word)
  32.     USRDSC    = SALT + 4        ; addr of username descriptor
  33.  
  34. ;
  35. ; Own Storage:
  36. ;
  37.  
  38. .psect    _LIB_CODE    RD, NOWRT, PIC, SHR, BYTE, EXE
  39.  
  40. ;
  41. ; Autodin-II polynomial table used by CRC algorithm
  42. ;
  43. AUTODIN:
  44.     .LONG ^X00000000, ^X1DB71064, ^X386E2008, ^X26D930AC
  45.     .LONG ^X76DC4190, ^X6B6B51F4, ^X4DB26158, ^X5005713C
  46.     .LONG ^XEDB88320, ^XF00F9344, ^XD6D6A3E8, ^XCB61B38C
  47.     .LONG ^X9B64C2B0, ^X86D3D2D4, ^XA00AE278, ^XBDBDF21C
  48.  
  49. ; The following table of coefficients is used by the Purdy polynomial
  50. ; algorithm.  They are prime, but the algorithm does not require this.
  51.  
  52. C:    .long    -83,    -1        ; C1
  53.     .long    -179,    -1        ; C2
  54.     .long    -257,    -1        ; C3
  55.     .long    -323,    -1        ; C4
  56.     .long    -363,    -1        ; C5
  57.  
  58. .SBTTL    Dispatch - select encryption algorithm
  59.  
  60. ;++
  61. ;
  62. ; Functional Description:
  63. ;
  64. ;    Smash up the password into a non-reversible number.
  65. ;
  66. ; Calling Sequence:
  67. ;
  68. ;    CALLS/CALLG
  69. ;
  70. ; Formal Parameters:
  71. ;
  72. ;    OUTDSC        Descriptor of quadword descriptor to contain
  73. ;            the results.
  74. ;    PWDDSC        Password descriptor
  75. ;    ENCRYPT        The encryption algorithm to be used
  76. ;    SALT        random number
  77. ;    USRDSC        Username descriptor
  78. ;--
  79.  
  80. .entry    LGI$HPWD,^M<R2, R3, R4, R5, R6>    ; entry mask
  81.  
  82.     tstb    ENCRYPT(ap)        ; using CRC algorithm
  83.     beql    20$            ; yes, no processing of usrdsc nesry
  84.     subl2    #20, sp            ; Get temp desc and buffer off stack
  85.     movl    sp, r6            ; put address in R6
  86.     movq    @USRDSC(ap), (r6)    ; put current userdesc on stack
  87.     cmpb    #1, ENCRYPT(AP)        ; which purdy algorithm
  88.     bneq    10$
  89.     movc5    (r6),@4(r6),#32,#12,8(r6) ; blank pad username
  90.     movw    #12,(r6)        ; force length 12
  91.     movab    8(r6),4(r6)        ; desc on stack point to stack
  92.     brb    20$            ; goto main line
  93.  
  94.                     ; PURDY_V. remove padding in username
  95. 10$:    movzwl    (r6),r5            ; save length of username
  96.     clrw    (r6)            
  97.     movl    4(r6),r0        ; get address of username buffer
  98. 15$:    cmpb    (r0)+,#32        ; search until we find first blank
  99.     beql    20$            ; found it
  100.     incw    (r6)            ; increment until byte found
  101.     cmpw    #31,(r6)        ; or 31 characters
  102.     beql    20$            ; (31 is max username length)
  103.     cmpw    r5,(r6)            ; or entire buffer has been parsed
  104.     beql    20$
  105.     brb    15$            ; loop
  106.  
  107. 20$:    movaq    @PWDDSC(ap),r4        ; if password is zero length
  108.     tstl    (r4)
  109.     bneq    25$            ; then return null password
  110.     movaq    @OUTDSC(ap),r4
  111.     clrw    (r4)
  112.     movc5    #0,(r4),#0,#8,@4(r4)    ; (quadword of zeros)
  113.     brb    40$
  114.  
  115. 25$:    movaq    @OUTDSC(ap),r4        ; get pointer to output buffer
  116.     movaq    @4(r4),r4        ; 
  117.     tstb    ENCRYPT(ap)        ; Use the CRC algorithm if the index
  118.     bgtru    30$            ; is zero
  119.     mnegl    #1, r0            ; initial CRC
  120.     movaq    @PWDDSC(ap),r1        ; get descriptor address
  121.     crc    AUTODIN,r0,(r1),@4(r1)    ; convert password to 32 bit number
  122.     clrl    r1            ; clear high order longword
  123.     movq    r0,(r4)            ; copy results to output buffer
  124.     brb    40$
  125.  
  126. 30$:    clrq    (r4)            ; initialize output buffer
  127.     movaq    @PWDDSC(ap),r3        ; Collapse password to quadword
  128.     bsbb    COLLAPSE_R2        ; 
  129.     addw2    SALT(ap),3(r4)        ; add random salt into middle of quad
  130.     movl    r6,r3            ; Collapse username into the quadword
  131.     bsbb    COLLAPSE_R2        ; 
  132.     pushaq    (r4)            ; push pointer to U
  133.     calls    #1,Purdy        ; Run U through poly mod P
  134.  
  135. 40$:    movl    #1,r0
  136.     ret
  137.  
  138. COLLAPSE_R2:
  139. .enabl    LSB
  140. ;++
  141. ; This routine takes a string of bytes (the descriptor for which is pointed
  142. ; to by r3) and collapses them into a quadword (pointed to by r4).  It does
  143. ; this by cycling aroun the bytes of the output buffer adding in the bytes
  144. ; of the input string
  145. ;--
  146.  
  147.     movzwl    (r3),r0            ; obtain the number of input bytes
  148.     beqlu    20$
  149.     moval    @4(r3),r2        ; Obtain pointer to input string
  150. 10$:    bicl3    #-8,r0,r1        ; Obtain cyclic index into output buf
  151.     addb2    (r2)+,(r4)[r1]
  152.     sobgtr    r0,10$            ; Loop until input string is exhausted
  153. 20$:    rsb
  154.  
  155. .SBTTL    Purdy - evaluate purdy polynomial
  156.  
  157. a = 59                    ; 2^64 - 59 is biggest quadword prime
  158.  
  159. n0 = 1@24 - 3                ; These exponents are prime but this
  160. n1 = 1@24 - 63                ; not required by the algorithm
  161.  
  162. .entry    Purdy, ^M<r2,r3,r4,r5>
  163. ;
  164. ; This routine computes f(U) = p(U) mod P. Where P is a prime of the form
  165. ; P = 2^64 - a.  The function P is the following polynomial:
  166. ; x^n0 + x^n1*C1 + x^3*C2 + x^2*C3 + x^2*C4 + C5
  167. ; The input U is an unsigned quadword
  168. ;
  169.  
  170.     pushq    @4(ap)            ; Push U
  171.     bsbw    PQMOD_R0        ; Ensure U less than P
  172.     movaq    (sp),r4            ; maintian a pointer to X
  173.     movaq    C,r5            ; Point to the table of coefficients
  174.     pushq    (r4)
  175.     pushl    #n1
  176.     bsbb    PQEXP_R3        ; X^n1
  177.     pushq    (r4)
  178.     pushl    #n0-n1            
  179.     bsbb    PQEXP_R3        ;
  180.     pushq    (r5)+            ; C1
  181.     bsbw    PQADD_R0        ; x^(n0-n1) + C1
  182.     bsbw    PQMUL_R2        ; x^n0 + x^N1*C1
  183.     pushq    (r5)+            ; C2
  184.     pushq    (r4)            ; 
  185.     bsbw    PQMUL_R2        ; x*C2
  186.     pushq    (r5)+            ; C3
  187.     bsbw    PQADD_R0        ; x*C2 + C3
  188.     pushq    (r4)            ; 
  189.     bsbb    PQMUL_R2        ; x^2*C2 + x*C3
  190.     pushq    (r5)+            ; C4
  191.     bsbw    PQADD_R0        ; x^2*C2 + x*C3 + c4
  192.     pushq    (r4)
  193.     bsbb    PQMUL_R2        ; x^3*C3 + X^2*C3 + x*C4
  194.     pushq    (r5)+
  195.     bsbw    PQADD_R0        ; x^3*C3 + X^2*C3 + x*C4 + C5
  196.     bsbw    PQADD_R0        ; add in the high order terms
  197.     popq    @4(ap)            ; replace U with F(x)
  198.     movl    #1,R0
  199.     ret
  200.  
  201. PQEXP_R3:
  202. .enabl    LSB
  203. ; replace the inputs with U^n mod P where P is of the form P = 2^64 - a
  204. ; U is a quadword, n is an unsigned longword.
  205.  
  206.     popr    #^M<r3>            ; record return address
  207.     pushq    #1            ; initalize
  208.     pushq    8+4(sp)            ; copy U to top of stack for speed
  209.     tstl    8+8(sp)            ; only handle n gtr 0
  210.     beqlu    30$
  211. 10$:    blbc    8+8(sp),20$
  212.     pushq    (sp)            ; Copy the current power of U
  213.     pushq    8+8(sp)            ; Multiply with current value
  214.     bsbb    PQMUL_R2        ; 
  215.     popq    8(sp)            ; Replace current value
  216.     cmpzv    #1,#31,8+8(sp),#0    ; 
  217.     beqlu    30$
  218. 20$:    pushq    (sp)            ; Proceed to next power of U
  219.     bsbb    PQMUL_R2        ;
  220.     extzv    #1,#31,8+8(sp),8+8(sp)    ;
  221.     brb    10$
  222. 30$:    movq    8(sp),8+8+4(sp)        ; copy the return value
  223.     movaq    8+8+4(sp),sp        ; discard exponent
  224.     jmp    (r3)            ; return
  225. .dsabl    LSB
  226.  
  227. u=0                    ; low longword of U
  228. v=u+4                    ; High longword of U
  229. y=u+8                    ; low longword of Y
  230. z=y+4                    ; High longword of Y
  231.  
  232. PQMOD_R0:
  233. .enabl    LSB
  234. ; Replaces the quadword U on the stack with U mod P where P is of the
  235. ; form 2^64 - a.
  236.  
  237.     popr    #^M<r0>
  238.     cmpl    v(sp),#-1
  239.     blssu    10$
  240.     cmpl    u(sp),#-a
  241.     blssu    10$
  242.     addl2    #a,u(sp)
  243.     adwc    #0,v(sp)
  244. 10$:    jmp    (r0)
  245. .dsabl    LSB
  246.  
  247. PQMUL_R2:
  248. ; computes the product U*Y mod P where P is of the form 2^64 - a.
  249. ; U, Y are quadwords less than P.  The product replaces U and Y on the stack.
  250.  
  251. ; The product may be formed as the sum of four longword multiplications
  252. ; which are scaled by powers of 2^32 by evaluating
  253. ; 2^64*v*z + 2^32*(v*y+u*z) + u*y
  254. ; The result is computed such that division by the modulus P is avoided
  255.  
  256.     popr    #^M<r1>            ; Record return address
  257.     movl    sp,r2
  258.     pushl    z(r2)
  259.     pushl    v(r2)
  260.     bsbb    EMULQ
  261.     bsbb    PQMOD_R0
  262.     bsb    PQLSH_R0
  263.     pushl    y(r2)
  264.     pushl    v(r2)
  265.     bsbb    EMULQ
  266.     bsbb    PQMOD_R0
  267.     pushl    z(r2)
  268.     pushl    u(r2)
  269.     bsbb    EMULQ
  270.     bsbb    PQMOD_R0
  271.     bsbb    PQADD_R0
  272.     bsbb    PQADD_R0
  273.     bsbb    PQLSH_R0
  274.     pushl    y(r2)
  275.     pushl    u(r2)
  276.     bsbb    EMULQ
  277.     bsbb    PQMOD_R0
  278.     bsbb    PQADD_R0
  279.     popq    y(r2)
  280.     movaq    y(r2),sp
  281.     jmp    (r1)
  282.  
  283. EMULQ:
  284. .enable LSB
  285. ; This routine knows how to multiply to unsigned longwords, replacing them
  286. ; with the unsigned quadword product on the stack.
  287.  
  288.     emul    4(sp),8(sp),#0,-(sp)
  289.     clrl    -(sp)
  290.     tstl    4+8+4(sp)        ; check both longwords to see if must
  291.     bgeq    10$            ; unsigned bias
  292.     addl    4+8+8(sp), (sp)
  293. 10$:    tstl    4+8+8(sp)
  294.     bgeq    20$
  295.     addl    4+8+4(sp),(sp)
  296. 20$:    addl    (sp)+,4(sp)
  297.     popq    4(sp)
  298.     rsb
  299. .dsabl    LSB
  300.  
  301. PQLSH_R0:
  302. .enabl    LSB
  303. ; Computes the product 2^32*U mod P where P is of the form P = 2^64 - a.
  304. ; U is a quadword less than P. the product replaces U on the stack.
  305.  
  306. ; This routine is used by PQMUL in the formation of quadword products in
  307. ; such a way as to avoid division by by the modulus P.
  308. ; The product 2^64*v + 2^32*u is congruent a*v + 2^32*u mod P (where u, v
  309. ; are longwords).
  310.  
  311.     popr    #^M<R0>            ; record return in r0
  312.     pushl    v(sp)
  313.     pushl    #a
  314.     bsbb    EMULQ            ; push a*v
  315.     ashq    #32,Y(sp),y(sp)        ; form y = 2^32*u
  316.     brb    10$
  317.  
  318. PQADD_R0:
  319. ; Computes the sum U + Y mod P where P is of the form P = 2^64 - a;
  320. ; U, Y are quadword less than P.  The sum replaces U and Y on the stack.
  321.     popr    #^M<r0>            ; Record return address
  322. 10$:    addl    u(sp),y(sp)        ; add the low longwords
  323.     adwc    v(sp),z(sp)        ; add the high longwords with carry
  324.     bcs    20$
  325.     cmpl    z(sp),#-1
  326.     blssu    30$
  327.     cmpl    y(sp),#-a
  328.     blssu    30$
  329. 20$:    addl2    #a,y(sp)
  330.     adwc    #0,z(sp)
  331. 30$:    movaq    y(sp),sp
  332.     jmp    (r0)
  333. .dsabl    LSB
  334.  
  335. .END
  336.